package com.polaris.live.dialog

import android.content.Context
import android.os.CountDownTimer
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.databinding.DialogNotHavaFaceBinding

/**
 * 没有人脸弹窗提示
 *
 * @author Created by 半仙 on 2023/10/23/023 16:35
 */
class NotHavaFaceDialog(context: Context, val dismiss: ((type: Boolean) -> Unit)) : CenterPopupView(

    context
) {

    private var time: TimeCount? = null
    private lateinit var mBinding: DialogNotHavaFaceBinding

    private var timing = true //判断是否正在倒计时  如果是并且调用onDismiss了就代表是外界主动结束弹窗  如果不是就代表内部主动结束弹窗
    //如果是外部主动结束就代表有人脸了   如果是内部结束就代表没有人脸

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_not_hava_face
    }

    override fun onCreate() {
        super.onCreate()
        mBinding = DialogNotHavaFaceBinding.bind(popupImplView)

    }

    override fun onShow() {
        super.onShow()
        time = TimeCount(10000, 1000)
        time?.start()
        mBinding.icBotton.text = "10"
    }

    override fun onDismiss() {
        if (time != null) {
            time?.cancel()
            time = null
        }
        dismiss.invoke(timing)
        mBinding.icBotton.text = "10"
        super.onDismiss()
    }

    internal inner class TimeCount(millisInFuture: Long, countDownInterval: Long) :
        CountDownTimer(millisInFuture, countDownInterval) {
        override fun onTick(millisUntilFinished: Long) {
            mBinding.icBotton.text =
                "${millisUntilFinished / 1000}"
        }


        override fun onFinish() {
            timing = false
            dismiss()
        }
    }
}